#ifndef CUSTOMSORTFILTERPROXYMODEL_H
#define CUSTOMSORTFILTERPROXYMODEL_H

#include <QObject>
#include <QSortFilterProxyModel>
#include <QWidget>

#include <QTableView>
#include <QString>
#include <QStandardItemModel>

class CustomSortFilterProxyModel : public QSortFilterProxyModel
{
public:
    explicit CustomSortFilterProxyModel(QObject *parent = nullptr);
protected:
    bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override
    {
        QVariant leftData = sourceModel()->data(source_left);
        QVariant rightData = sourceModel()->data(source_right);


        QString leftString = leftData.toString();
        QString rightString = rightData.toString();
        // 检查是否以大写字母 "F" 开头
        if (leftString.startsWith("F") && rightString.startsWith("F")) {
            // 提取数字部分
            int leftNumber = extractNumber(leftString);
            int rightNumber = extractNumber(rightString);

            return leftNumber < rightNumber;
        } else {
            // 默认的字符串排序
            return QString::compare(leftString, rightString,Qt::CaseInsensitive) < 0;
        }
    }

    /**
     * 追加headerData函数
     * @brief headerData
     * @param section
     * @param orientation
     * @param role
     * @return
     */
    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override
    {
        //如果是垂直表头，不到sourcemodel（数据model里面去找了），因为找数据model里面就是数据model的行号。所以才会跟着变化。
        if(Qt::Vertical == orientation && Qt::DisplayRole == role) {
            return QString::number(section +1);    //section是从0开始的
        }
        //其他表头的内容还是从sourcemodel去找
        return sourceModel()->headerData(section,orientation,role);

        // if(role != Qt::DisplayRole){
        //     return QVariant();
        // }
        //水平
        if(orientation == Qt::Horizontal){
            switch(section){
            case 0:
                return "序号";
            case 1:
                return "参数";
            case 2:
                return "快捷项";
            case 3:
                return "快捷名称";
            case 4:
                return "命令行";
            default:
                return "";
            }
        }else{
            return QString("%1").arg(section + 1);
        }
    }


private:
    int extractNumber(const QString &str) const
    {
        // 假设字符串格式为 "F<数字>"
        QString numberPart = str.mid(1); // 从第二个字符开始提取数字部分
        return numberPart.toInt();
    }
};

#endif // CUSTOMSORTFILTERPROXYMODEL_H
